package com.forever7776.life.core.service.impl.shiro;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.cache.RedisCacheOperator;
import com.forever7776.life.core.common.constants.RedisCacheConstants;
import com.forever7776.life.core.entity.shiro.ShiroAuthoritySetting;
import com.forever7776.life.core.mapper.shiro.ShiroAuthoritySettingMapper;
import com.forever7776.life.core.service.shiro.IShiroAuthoritySettingService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-01-18
 */
@Service
public class ShiroAuthoritySettingServiceImpl extends ServiceImpl<ShiroAuthoritySettingMapper, ShiroAuthoritySetting> implements IShiroAuthoritySettingService {

    @Autowired
    private ShiroAuthoritySettingMapper shiroAuthoritySettingMapper;

    @Autowired
    private RedisCacheOperator redisCacheOperator;


    /**
     * 查询shiro拦截的权限
     *
     * @param status
     * @return
     */
    @Override
    public List<String> getUrls(Integer status) {
        List<String> shiroSettingUrls = new ArrayList<>();
        try {
            List<String> cacheShiroSettingUrls = redisCacheOperator.getCacheList(RedisCacheConstants.GLOBAL_SHIRO_SETTING_URLS);
            if (CollectionUtils.isNotEmpty(cacheShiroSettingUrls)) {
                return cacheShiroSettingUrls;
            }
            shiroSettingUrls = shiroAuthoritySettingMapper.getUrls(status);
            redisCacheOperator.setCacheList(RedisCacheConstants.GLOBAL_SHIRO_SETTING_URLS, shiroSettingUrls, 7, TimeUnit.DAYS);
        } catch (Exception e) {
            shiroSettingUrls = shiroAuthoritySettingMapper.getUrls(status);
        }
        return shiroSettingUrls;
    }
}
